/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * ContractItemServiceImpl.java
 * business
 * 2013-5-22下午3:43:34
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jackysoft.config.CachedQuery;
import org.jackysoft.entity.ContractItem;
import org.jackysoft.query.Query;
import org.jackysoft.query.QueryState;
import org.jackysoft.service.AbstractStringIdServiceImpl;
import org.jackysoft.service.ContractItemService;
import org.jackysoft.util.Page;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Maps;

@Service
@Transactional
public class ContractItemServiceImpl extends
		AbstractStringIdServiceImpl<ContractItem> implements
		ContractItemService {

		
	/**
	 * 
	 */
	private static final long serialVersionUID = 2474807997119746536L;

	
	private CachedQuery cacheQuery;
	
	public ContractItemServiceImpl() {
	    super(ContractItem.class);
	   
	}

	@Override
	public Page query(Query qc, Integer pindex, Integer rows)
			throws DataAccessException {
					
		if(qc.getState()==QueryState.PAGE){			
			if(cacheQuery!=null) qc = cacheQuery;			
		}else if(qc.getState()==QueryState.START){			
			
			cacheQuery.setSqlId(qc.getSqlId());
			cacheQuery.setResultCode(qc.getResultCode());
			cacheQuery.setFields(qc.getFields());
			cacheQuery.setExpress(qc.getExpress());
			cacheQuery.setGroupBy(qc.getGroupBy());
			cacheQuery.setOrders(qc.getOrders());
		
		}
		log.info("缓存的sqlID ："+cacheQuery.getResultCode());
		log.info("查询对象sqlId: "+qc.getResultCode());
		return details(qc, pindex, rows);
	}
	
	

	@Override
	public Page details(Query qc, Integer pindex, Integer rows) {
		log.info(qc.queryString());
		int start = pindex*rows;
	    if(start<0) start=0;
        String sqlId = qc.getSqlId();
		Map<String,String> parameter = Maps.newHashMap();
		parameter.put("cond", qc.queryString());
		parameter.put("page", start+"");
		parameter.put("rows", rows+"");		
		Integer count = this.sqlSession.selectOne(sqlId+"Counter",parameter);
		List<Map<String,Object>> results = this.sqlSession.selectList(sqlId, parameter);		
		Page page = new Page();
		page.setOffset(rows);
		page.setPage(pindex);
		page.setRecords(count);
		page.setRows(results);
		return page;
	}
	
	


    @Override
    public Page collect(Query query) throws DataAccessException {
        Page page = new Page();
        String sqlId = query.getSqlId();
        Map<String,String> parameter = Maps.newHashMap();
		parameter.put("cond", query.queryString());
        List<Map<String,Object>> results = sqlSession.selectList(sqlId,parameter);
        page.setRows(results);
        return page;
    }

    @Resource
    public void setCacheQuery(CachedQuery cacheQuery) {
        this.cacheQuery = cacheQuery;
    }

	@Override
	public int countByExpress(Query query) {
		Map<String,String> parameter = Maps.newHashMap();
		parameter.put("express", query.getExpress());		
		parameter.put("cond", query.queryString());		
		Integer count = sqlSession.selectOne(query.getSqlId(),parameter);
		return count;
	}
}
